﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ThreeTeams.API.Application.Command.ProductCommand;
using ThreeTeams.Domain.ThreeTeams.Domain;
using ThreeTeams.ErrorCode;

namespace ThreeTeams.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        private readonly IMediator mediator;

        public ProductController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 获取货物列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<ApiPaging<Product>>> GetProduct([FromQuery] GetProductCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 添加产品
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> CreateProduct(CreateProductCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 删除产品
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult<int>> DelProduct(DeleteProductCommand request)
        {
            return await mediator.Send(request);
        }
        /// <summary>
        /// 修改产品
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateProduct(UpdateProductCommand request)
        {
            return await mediator.Send(request);
        }
        [HttpPost]
        public IActionResult UpFile(IFormFile file)
        {
            var d = Directory.GetCurrentDirectory() + @"\wwwroot\pic\" + file.FileName;
            using (FileStream fs = new FileStream(d, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            return Ok("http://localhost:5118/pic/" + file.FileName);
        }


    }
}
